<?php

/*
 * This class is used for as Model for Calculator table 
 */

class Model_Calculator extends Model_Base {

    protected $_name = 'tbl_calculator';
    protected $_primaryKey = 'cal_id';

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('calculator' => 'tbl_calculator'))
                ->where('cal_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $calculatorData = $this->fetchRow($select);


        if (!empty($calculatorData)) {
            $calculator = $calculatorData->toArray();

            $cal_id = $calculator['cal_id'];

            //Load Calculator Reference Model
            $calculatorReferenceModel = new Model_Calculatorreference();
            $calculatorReference = $calculatorReferenceModel->fetchEntryById($cal_id);

            return Array(
                'CALCULATOR' => $calculator,
                'CALCULATOR_REFERENCE' => $calculatorReference,
            );
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get calculators
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchCalculators($where = "", $limit = "", $offset = "0", $order = 'cal_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('calculator' => 'tbl_calculator'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            $calculatorData = $data->toArray();
            $finalArray = Array();
            foreach ($calculatorData as $_calc) {
                $cal_id = $_calc['cal_id'];

                //Load Calculator Reference Model
                $calculatorReferenceModel = new Model_Calculatorreference();
                $calculatorReference = $calculatorReferenceModel->fetchEntryById($cal_id);

                $finalArray[$cal_id] = Array(
                    'CALCULATOR' => $_calc,
                    'CALCULATOR_REFERENCE' => $calculatorReference,
                );
            }
            return $finalArray;
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total calculator
     * @return int
     */
    public function getCalculatorCount($where) {
        $select = $this->select()
                ->from(array('calculator' => 'tbl_calculator'), Array('cnt' => 'count(*)'));
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete calculators
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteCalculator($calculatortArray) {
        if (count($calculatortArray) <= 0) {
            return;
        }
        foreach ($calculatortArray as $_calculator) {
            $calculatorData = $this->fetchEntryById($_calculator);
            $calculator = $calculatorData['QUOTATION'];

            //Delete Calculator table entry
            $this->delete('cal_id = ' . (int) $calculator['cal_id']);

            //Load Calculator referece table 
            $calculatorReferenceModel = new Model_Calculatorreference();
            //Delete Calculator Reference table entry
            $calculatorReferenceModel->delete('cal_id = ' . (int) $calculator['cal_id']);
        }
    }

}

